public class Leetcode1750 {
    public static void main(String[] args) {
        System.out.println(minimumLength("bbbbbbbbbbbbbbbbbbbbbbbbbbbabbbbbbbbbbbbbbbccbcbcbccbbabbb"));
        System.out.println(minimumLength("ca"));
        System.out.println(minimumLength("cabaabac"));
        System.out.println(minimumLength("aabccabba"));
    }

    public static int minimumLength(String s) {
        return minimumLength(s.toCharArray(), 0, s.length() - 1);
    }

    public static int minimumLength(char[] s, int start, int end) {
        if (end == start) {
            return 1;
        }

        if (end < start) {
            return 0;
        }

        char tmp = s[start];

        if (tmp != s[end]) {
            return end - start + 1;
        }

        while (start < s.length && s[start] == tmp) {
            start++;
        }

        while (end >= 0 && tmp == s[end]) {
            end--;
        }

        return minimumLength(s, start, end);
    }

}
